package com.project.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.project.bean.VendorBean;
import com.project.service.IVendorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("excel")
public class ExcelController {
    @Autowired
    private IVendorService vendorService;
    @RequestMapping("add")
    public String add(MultipartFile excelFile) throws Exception {
        //判断文件是否为空
        if (!excelFile.isEmpty()) {
            ImportParams params = new ImportParams();
            //设置选项卡
            params.setSheetNum(1);
            //标题栏占用几行
            params.setTitleRows(1);
            //表头占用几行
            params.setHeadRows(1);
            //第一个参：上传文件的二进制流，第二个：实体的类模板，第三个是：初始化信息
            List<VendorBean> list = ExcelImportUtil.importExcel(excelFile.getInputStream(), VendorBean.class, params);

            for (VendorBean v:list){
                vendorService.addVendor(v);
            }
            return "ok";

        }
        return null;
    }

}
